package com.losdun.points.controller.admin;

import com.losdun.common.core.controller.BaseController;
import com.losdun.common.core.domain.AjaxResult;
import com.losdun.common.core.page.TableDataInfo;
import com.losdun.points.controller.req.PointsEditReq;
import com.losdun.points.controller.req.UserPointsReq;
import com.losdun.points.domain.PointsRecord;
import com.losdun.points.domain.UserPoints;
import com.losdun.points.enums.PointsSourceTypeEnum;
import com.losdun.points.service.PointsRecordService;
import com.losdun.points.service.UserPointsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description
 * @Author Sword.Ye
 * @Date 2025/11/17
 **/
@RestController
@RequestMapping("/points/userPoints")
@Slf4j
public class PointsRecordController extends BaseController {

    @Autowired
    private PointsRecordService pointsRecordService;
    @Autowired
    private UserPointsService userPointsService;

    @PreAuthorize("@ss.hasPermi('points:userPoints:list')")
    @GetMapping("/userList")
    public TableDataInfo queryUserPointsList(UserPointsReq req){
        startPage();
        List<UserPoints> userPointsList = userPointsService.queryUserPointsList(req);
        return getDataTable(userPointsList);
    }
    @GetMapping("/recordList")
    @PreAuthorize("@ss.hasPermi('points:userPoints:record')")
    public AjaxResult queryUserPointsList(PointsRecord record){
        List<PointsRecord> list = pointsRecordService.queryPointsRecordList(record);
        return AjaxResult.success(list);
    }

    @PreAuthorize("@ss.hasPermi('points:userPoints:update')")
    @PostMapping("/update")
    public AjaxResult update(@RequestBody PointsEditReq req){
        try {
            pointsRecordService.updateUserPointsRecord(req.getWxUserId(), PointsSourceTypeEnum.SYSTEM.getCode(),req.getType(),req.getPoints(),req.getRemark());
        } catch (Exception e) {
            log.error("更新用户积分失败！",e);
            return error("更新用户积分失败！");
        }
        return success();
    }

}
